//
//  HomeTableViewCell.swift
//  SwiftStory
//
//  Created by ZxjMacBookPro on 2020/5/13.
//  Copyright © 2020 ZxjMacBookPro. All rights reserved.
//

import UIKit
import SDWebImage

class HomeTableViewCell: UITableViewCell {

    /// 头像
    @IBOutlet weak var iconImageView: UIImageView!
    /// 认证图标
    @IBOutlet weak var verifiedImageView: UIImageView!
    /// 昵称
    @IBOutlet weak var nameLabel: UILabel!
    /// 会员图标
    @IBOutlet weak var vipImageView: UIImageView!
    /// 时间
    @IBOutlet weak var timeLabel: UILabel!
    /// 来源
    @IBOutlet weak var sourceLabel: UILabel!
    /// 正文
    @IBOutlet weak var contentLabel: UILabel!
    ///图片容器
    @IBOutlet weak var pictureCollectionView: HomeCollectionView!
    ///底部视图
    @IBOutlet weak var footerView: UIView!
    ///转发正文
    @IBOutlet weak var forwardLB: UILabel!
    
    /// 模型数据
    var viewModel: StatusViewModel?
    {
        didSet{
            
            // 1.设置头像
            iconImageView.sd_setImage(with: viewModel?.icon_URL, completed: nil)
            // 2.设置认证图标
            verifiedImageView.image = viewModel?.verified_image
            
            // 3.设置昵称
            nameLabel.text = viewModel?.status.user?.screen_name
            
            // 4.设置会员图标
            vipImageView.image = nil
            nameLabel.textColor = UIColor.black
            if let image = viewModel?.mbrankImage
            {
                vipImageView.image = image
                nameLabel.textColor = UIColor.orange
            }
            // 5.设置时间
            timeLabel.text = viewModel?.created_Time
            
            // 6.设置来源
            sourceLabel.text = viewModel?.source_Text
            
            // 7.设置正文
            contentLabel.text = viewModel?.status.text

            pictureCollectionView.viewModel = viewModel
            
            // 10.转发微博
            let text = viewModel?.forwardText
            if !text!.isEmpty {
                forwardLB.text = text
                forwardLB.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 2 * 10
            }
        }
    }
    

    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        contentLabel.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 2 * 10
        pictureCollectionView.register(HomePictureCell.self, forCellWithReuseIdentifier: "pictureCell")
        // Initialization code
    }
    
    // MARK: - 外部控制方法
    func calculateRowHeight(viewModel: StatusViewModel) -> CGFloat {
        self.viewModel = viewModel
        self.layoutIfNeeded()
        
        return footerView.frame.maxY
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}

extension HomeTableViewCell: UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel?.thumbnail_pic?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "pictureCell", for: indexPath) as! HomePictureCell
        cell.backgroundColor = UIColor.red
        
        cell.url = viewModel!.thumbnail_pic![indexPath.item]
        
        return cell
    }
}



